<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
      label-width="68px"
    >
      <el-form-item label="ID" prop="id">
        <el-input
          v-model="queryParams.id"
          placeholder="请输入ID"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="通用名称" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入通用名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="其他别称" prop="alias">
        <el-input
          v-model="queryParams.alias"
          placeholder="请输入其他别称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="厂家" prop="manuId">
        <el-select
          v-model="queryParams.manuId"
          placeholder="请选择厂家"
          clearable
        >
          <el-option
            v-for="dict in dict.type.drug_factory"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>

      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['ydz:drug:add']"
          >新增</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['ydz:drug:edit']"
          >修改</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['ydz:drug:remove']"
          >删除</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['ydz:drug:export']"
          >导出</el-button
        >
      </el-col>
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-table
      v-loading="loading"
      :data="drugList"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="主键" align="center" prop="id" />
      <el-table-column label="通用名称" align="left" prop="name" />
      <el-table-column label="其他别称" align="left" prop="alias" />
      <el-table-column label="主图" align="center" prop="icon" width="100">
        <template slot-scope="scope">
          <image-preview :src="scope.row.icon" :width="50" :height="50" />
        </template>
      </el-table-column>
      <el-table-column label="厂家" align="center" prop="manuId">
        <template slot-scope="scope">
          <dict-tag
            :options="dict.type.drug_factory"
            :value="scope.row.manuId"
          />
        </template>
      </el-table-column>
      <!-- <el-table-column label="是否推荐" align="center" prop="recommend" /> -->
      <el-table-column label="创建人名称" align="center" prop="createName" />
      <el-table-column
        label="创建时间"
        align="center"
        prop="createTime"
        width="180"
      >
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime, "{y}-{m}-{d}") }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['ydz:drug:edit']"
            >修改</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['ydz:drug:remove']"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改药品对话框 -->
    <el-dialog
      :title="title"
      :visible.sync="open"
      width="60%"
      append-to-body
      :close-on-click-modal="false"
    >
      <el-tabs v-model="activeName" type="card">
        <el-tab-pane label="药品信息" name="first">
          <el-form ref="form" :model="form" :rules="rules" label-width="120px">
            <el-form-item label="主图" prop="icon">
              <image-upload v-model="form.icon" />
            </el-form-item>
            <!-- <el-row :gutter="50">
              <el-col :span="12">
                <el-form-item label="中国是否上市" prop="chinaListedState">
                  <el-radio-group v-model="form.chinaListedState">
                    <el-radio
                      v-for="dict in dict.type.is_yes_no"
                      :key="dict.value"
                      :label="parseInt(dict.value)"
                      >{{ dict.label }}</el-radio
                    >
                  </el-radio-group>
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item label="是否进入医保" prop="enterMedicalState">
                  <el-radio-group v-model="form.enterMedicalState">
                    <el-radio
                      v-for="dict in dict.type.is_yes_no"
                      :key="dict.value"
                      :label="parseInt(dict.value)"
                      >{{ dict.label }}</el-radio
                    >
                  </el-radio-group>
                </el-form-item>
              </el-col>
            </el-row>
            <el-row :gutter="50">
              <el-col :span="12">
                <el-form-item label="是否有仿制药" prop="genericState">
                  <el-radio-group v-model="form.genericState">
                    <el-radio
                      v-for="dict in dict.type.is_yes_no"
                      :key="dict.value"
                      :label="parseInt(dict.value)"
                      >{{ dict.label }}</el-radio
                    >
                  </el-radio-group>
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item label="国内是否能买到" prop="canChinaState">
                  <el-radio-group v-model="form.canChinaState">
                    <el-radio
                      v-for="dict in dict.type.is_yes_no"
                      :key="dict.value"
                      :label="parseInt(dict.value)"
                      >{{ dict.label }}</el-radio
                    >
                  </el-radio-group>
                </el-form-item>
              </el-col>
            </el-row> -->
            <el-row :gutter="50">
              <el-col :span="12">
                <el-form-item label="药品疾病" prop="drugDiseaseList">
                  <el-select
                    v-model="form.drugDiseaseList"
                    placeholder="请药品疾病"
                    value-key="diseaseId"
                    filterable
                    multiple
                    @change="handleRemoveTag"
                  >
                    <el-option
                      v-for="item in diseaseList"
                      :key="item.id"
                      :label="item.name"
                      :value="{ diseaseId: item.id }"
                    ></el-option>
                  </el-select>
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item label="是否处方药" prop="isPrescription">
                  <el-radio-group v-model="form.isPrescription">
                    <el-radio
                      v-for="dict in dict.type.is_yes_no"
                      :key="dict.value"
                      :label="parseInt(dict.value)"
                      >{{ dict.label }}</el-radio
                    >
                  </el-radio-group>
                </el-form-item>
              </el-col>
            </el-row>

            <el-row :gutter="50">
              <!-- <el-col :span="12">
                <el-form-item label="准批文号" prop="quasiApprovalNo">
                  <el-input
                    v-model="form.quasiApprovalNo"
                    placeholder="请输入准批文号"
                  />
                </el-form-item>
              </el-col> -->
              <el-col :span="12">
                <el-form-item label="权重" prop="salesvolume">
                  <el-input
                    v-model="form.salesvolume"
                    placeholder="请输入权重"
                  />
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item label="发布状态" prop="state">
                  <el-radio-group v-model="form.state">
                    <el-radio
                      v-for="dict in dict.type.is_yes_no"
                      :key="dict.value"
                      :label="dict.value"
                      >{{ dict.label }}</el-radio
                    >
                  </el-radio-group>
                </el-form-item>
              </el-col>

              
            </el-row>
            <!-- <el-row :gutter="50">
              <el-col :span="12">
                <el-form-item label="适用人群" prop="suitableforthecroud">
                  <el-input
                    v-model="form.suitableforthecroud"
                    placeholder="请输入内容"
                  />
                </el-form-item>
              </el-col>
            </el-row> -->
            <el-form-item label="适应症">
              <editor v-model="form.adaptationdisease1" :min-height="192" />
            </el-form-item>
            <el-form-item label="用法用量">
              <editor v-model="form.usageanddosage" :min-height="192" />
            </el-form-item>
            <el-form-item label="治疗效果">
              <editor v-model="form.therapeuticEffect" :min-height="192" />
            </el-form-item>
            <el-form-item label="上市信息">
              <editor v-model="form.adaptationdisease" :min-height="192" />
            </el-form-item>
            <el-form-item label="副作用">
              <editor v-model="form.untoward" :min-height="192" />
            </el-form-item>
            <el-form-item label="注意事项">
              <editor v-model="form.announcements" :min-height="192" />
            </el-form-item>
            <el-form-item label="特殊人群用药">
              <editor v-model="form.suitableforthecroud1" :min-height="192" />
            </el-form-item>
            <el-form-item label="药物相互作用">
              <editor v-model="form.mutual" :min-height="192" />
            </el-form-item>
          </el-form>
        </el-tab-pane>

        <el-tab-pane label="商品相册" name="first1">
          <el-form ref="form" :model="form" :rules="rules" label-width="80px">
            <el-form-item label="商品相册" prop="photoalbum">
              <image-upload v-model="form.photoalbum" />
            </el-form-item>
          </el-form>
        </el-tab-pane>
        <el-tab-pane label="药品百科" name="first2">
          <el-form ref="form" :model="form" :rules="rules" label-width="120px">
            <el-row :gutter="50">
              <el-col :span="12">
                <el-form-item label="名称" prop="name">
                  <el-input v-model="form.name" placeholder="请输入名称" />
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item label="通用名称" prop="alias">
                  <el-input
                    v-model="form.commonName"
                    placeholder="请输入通用名称"
                  />
                </el-form-item>
              </el-col>
            </el-row>
            <el-row :gutter="50">
              <el-col :span="12">
                <el-form-item label="商品名称" prop="name">
                  <el-input
                    v-model="form.englishName"
                    placeholder="请输入商品名称"
                  />
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item label="其他别称" prop="alias">
                  <el-input v-model="form.alias" placeholder="请输入其他别称" />
                </el-form-item>
              </el-col>
            </el-row>
            <el-form-item label="适应人群">
              <editor v-model="form.intendedPopulation" :min-height="192" />
            </el-form-item>
            <el-row :gutter="50">
              <!-- <el-col :span="12">
                <el-form-item label="适应靶点" prop="adaptiveTarget">
                  <el-input
                    v-model="form.adaptiveTarget"
                    placeholder="请输入适应靶点"
                  />
                </el-form-item>
              </el-col> -->
              <el-col :span="12">
                <el-form-item label="主要成分" prop="ingredient1">
                  <el-input
                    v-model="form.ingredient1"
                    placeholder="请输入主要成分"
                  />
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item label="剂型" prop="dosage">
                  <el-input v-model="form.dosage" placeholder="请输入剂型" />
                </el-form-item>
              </el-col>
            </el-row>
            <!-- <el-row :gutter="50">
              <el-col :span="12">
                <el-form-item label="适应人群" prop="intendedPopulation">
                  <el-input
                    v-model="form.intendedPopulation"
                    placeholder="请输入适应人群"
                  />
                </el-form-item>
              </el-col>
            </el-row> -->

            <el-row :gutter="50">
              <el-col :span="12">
                <el-form-item label="包装与规格" prop="packaging">
                  <el-input v-model="form.packaging" placeholder="请输入包装" />
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item label="厂家" prop="manuId">
                  <el-select v-model="form.manuId" placeholder="请选择厂家">
                    <el-option
                      v-for="dict in dict.type.drug_factory"
                      :key="dict.value"
                      :label="dict.label"
                      :value="dict.value"
                    ></el-option>
                  </el-select>
                </el-form-item>
              </el-col>
            </el-row>
            <el-row :gutter="50">
              <el-col :span="12">
                <el-form-item label="储存方法" prop="storagecondition">
                  <el-input
                    v-model="form.storagecondition"
                    placeholder="请输入内容"
                  />
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item label="有效期" prop="periodOfValidity">
                  <el-input
                    v-model="form.periodOfValidity"
                    placeholder="请输入有效期"
                  />
                </el-form-item>
              </el-col>
            </el-row>

            <!-- <el-form-item label="性状">
              <editor v-model="form.xingzhuang" :min-height="192" />
            </el-form-item> -->
            <el-form-item label="药品概述">
              <editor v-model="form.usageanddosage1" :min-height="192" />
            </el-form-item>
            <el-form-item label="说明书">
              <editor v-model="form.untoward1" :min-height="192" />
            </el-form-item>
            <!-- <el-form-item label="注意事项">
              <editor v-model="form.announcements1" :min-height="192" />
            </el-form-item>
            <el-form-item label="特殊人群用药">
              <editor v-model="form.suitableforthecroud1" :min-height="192" />
            </el-form-item>
            <el-form-item label="禁忌症">
              <editor v-model="form.taboos1" :min-height="192" />
            </el-form-item>
            <el-form-item label="药物相互作用">
              <editor v-model="form.mutual1" :min-height="192" />
            </el-form-item> -->

            
            <!-- <el-form-item label="药物过量" prop="overdose">
              <el-input
                v-model="form.overdose"
                type="textarea"
                placeholder="请输入内容"
              />
            </el-form-item>
            <el-form-item label="药代动力学" prop="pharmacokinetics">
              <el-input
                v-model="form.pharmacokinetics"
                type="textarea"
                placeholder="请输入内容"
              />
            </el-form-item> -->
          </el-form>
        </el-tab-pane>
      </el-tabs>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listDrug, getDrug, delDrug, addDrug, updateDrug } from "@/api/drug";
import {
  listDisease,
  getDisease,
  delDisease,
  addDisease,
  updateDisease,
} from "@/api/disease";

export default {
  name: "Drug",
  dicts: ["drug_factory", "is_yes_no"],

  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 药品表格数据
      drugList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        id: null,
        pageNum: 1,
        pageSize: 10,
        name: null,
        alias: null,
        icon: null,
        manuId: null,
        manuName: null,
        periodOfValidity: null,
        quasiApprovalNo: null,
        dosage: null,
        editionPrice: null,
        isPrescription: null,
        recommend: null,
        storagecondition: null,
        announcements: null,
        adaptationdisease: null,
        photoalbum: null,
        detail: null,
        videos: null,
        ingredient: null,
        xingzhuang: null,
        usageanddosage: null,
        untoward: null,
        taboos: null,
        salesvolume: null,
        suitableforthecroud: null,
        mutual: null,
        hits: null,
        chinaListedState: null,
        enterMedicalState: null,
        genericState: null,
        canChinaState: null,
        englishName: null,
        commonName: null,
        adaptiveTarget: null,
        packaging: null,
        overdose: null,
        pharmacokinetics: null,
        mutual1: null,
        usageanddosage1: null,
        untoward1: null,
        announcements1: null,
        suitableforthecroud1: null,
        taboos1: null,
        adaptationdisease1: null,
        ingredient1: null,
        intendedPopulation: null,
        therapeuticEffect: null,
        webtitle: null,
        description: null,
        createName: null,
        orderByColumn: "updateTime",
        isAsc: "DESC",
      },
      // 表单参数
      form: {
        drugDiseaseList: null,
      },
      // 表单校验
      rules: {},
      activeName: "first",
      diseaseList: null,
      //疾病删除数组
      delDrugDiseaseIds: [],
      //回显时疾病数组，用来删除比对
      drugDis: [],
    };
  },
  created() {
    this.getList();
    this.listDisease();
  },
  methods: {
    // handleRemoveTag(value) {
    //   this.drugDis.forEach((element) => {
    //     if (element.diseaseId == value.diseaseId) {
    //       this.delDrugDiseaseIds.push(element.id);
    //       if (!this.delDrugDiseaseIds.includes(element.id)) {
    //         this.delDrugDiseaseIds.push(element.id);
    //       }
    //       return;
    //     }
    //   });
    // },
    handleRemoveTag(value) {
      this.drugDis
        .filter(
          (item1) => !value.some((item2) => item2.diseaseId === item1.diseaseId)
        )
        .forEach((item) => {
          if (!this.delDrugDiseaseIds.includes(item.id)) {
            this.delDrugDiseaseIds.push(item.id);
          }
        });
    },
    /** 查询药品列表 */
    getList() {
      this.loading = true;
      listDrug(this.queryParams).then((response) => {
        this.drugList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    listDisease() {
      let params = {
        pageNum: 1,
        pageSize: 10000,
      };
      listDisease(params).then((response) => {
        this.diseaseList = response.data;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        name: null,
        alias: null,
        icon: null,
        remark: null,
        state: null,
        manuId: null,
        manuName: null,
        periodOfValidity: null,
        quasiApprovalNo: null,
        dosage: null,
        editionPrice: null,
        isPrescription: null,
        recommend: null,
        storagecondition: null,
        announcements: null,
        adaptationdisease: null,
        photoalbum: null,
        detail: null,
        videos: null,
        ingredient: null,
        xingzhuang: null,
        usageanddosage: null,
        untoward: null,
        taboos: null,
        salesvolume: null,
        suitableforthecroud: null,
        mutual: null,
        hits: null,
        chinaListedState: null,
        enterMedicalState: null,
        genericState: null,
        canChinaState: null,
        englishName: null,
        commonName: null,
        adaptiveTarget: null,
        packaging: null,
        overdose: null,
        pharmacokinetics: null,
        mutual1: null,
        usageanddosage1: null,
        untoward1: null,
        announcements1: null,
        suitableforthecroud1: null,
        taboos1: null,
        adaptationdisease1: null,
        ingredient1: null,
        intendedPopulation: null,
        therapeuticEffect: null,
        webtitle: null,
        description: null,
        keywords: null,
        tdkTitle: null,
        tdkDescribe: null,
        createBy: null,
        createName: null,
        createTime: null,
        updateBy: null,
        updateName: null,
        updateTime: null,
        delFlag: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加药品";
      this.activeName = "first";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids;
      this.activeName = "first";

      getDrug(id).then((response) => {
        this.form = response.data;
        this.open = true;
        this.title = "修改药品";
        this.form.drugDiseaseList = response.data.drugDiseaseList;
        this.drugDis = response.data.drugDiseaseList;
        this.delDrugDiseaseIds = [];
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.form.delDrugDiseaseIds = this.delDrugDiseaseIds;

      this.$refs["form"].validate((valid) => {
        if (valid) {
          console.log("this.form", this.form);
          if (this.form.id != null) {
            updateDrug(this.form).then((response) => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addDrug(this.form).then((response) => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal
        .confirm('是否确认删除药品编号为"' + ids + '"的数据项？')
        .then(function () {
          return delDrug(ids);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "ydz/drug/export",
        {
          ...this.queryParams,
        },
        `drug_${new Date().getTime()}.xlsx`
      );
    },
  },
};
</script>
<style scoped>
::v-deep .el-dialog {
  height: 78vh;
  overflow: auto;
}

::v-deep .el-upload-dragger {
  width: 146px;
  height: 146px;
  border: none;
}

::v-deep .vue-treeselect__x-container {
  display: none;
}
::v-deep .el-input--medium .el-input__inner {
  width: 380px;
}
.box /deep/ .el-input--medium .el-input__inner {
  width: 380px;
}
.box /deep/ .vue-treeselect__control {
  width: 300px;
}
.page /deep/ .el-input__inner {
  width: 100%;
}
.order /deep/ .el-input--medium .el-input__inner {
  width: 837px;
}
.drug /deep/ .el-input--medium .el-input__inner {
  width: 130px;
}
.drugtw /deep/ .el-input--medium .el-input__inner {
  width: 200px;
}
::v-deep .el-table__header {
  width: auto !important;
}
::v-deep .ordertable .el-input--medium .el-input__inner {
  width: inherit;
}
</style>